package com.kindnesslibrary.base;

import android.app.Application;
import android.graphics.Bitmap;

import com.kindnesslibrary.R;
import com.kindnesslibrary.network.HttpConfig;
import com.kindnesslibrary.util.AlbumHelper;
import com.kindnesslibrary.util.Contants;
import com.kindnesslibrary.util.MyUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.io.File;
import java.util.concurrent.ExecutorService;

/**
 * Created by chenhongxin on 2017/5/9.
 */
public abstract class BaseApplication extends Application {

    private InitClass initClass;
    private static BaseApplication baseApplication;
    public DisplayImageOptions options;
    /**
     * 状态栏颜色
     */
    public static int statusBarColor;

    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication = this;
        initClass = initParameter();
        statusBarColor = initStatusBarColor();
        HttpConfig.getInstance().init(initClass.getDomain());
        AlbumHelper.getInstance().init(this);
        initImageLoader();
    }

    private void initImageLoader() {
        Runtime mRuntime = Runtime.getRuntime();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.waitnet)
                .showImageForEmptyUri(R.mipmap.waitnet)
                .showImageOnFail(R.mipmap.waitnet).cacheInMemory(true)
                .cacheOnDisk(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                // RoundedBitmapDisplayer(0)参数是圆角半径
                .build();
        File file = MyUtil.getInstance().getCache(this,
                Contants.IMAGE_CACHE_PATH);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCache(new UnlimitedDiscCache(file))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .memoryCacheSize((int) (mRuntime.maxMemory() / 3))
                .threadPoolSize(initClass.getAvailableSize())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);
    }

    /**
     * 初始化必要参数
     * @return 必要参数包
     */
    protected abstract InitClass initParameter();
    /**
     * 初始化状态栏颜色
     * @return
     */
    protected abstract int initStatusBarColor();

    /**
     * 获取Application实例
     * @return
     */
    public static BaseApplication getInstance(){
        return baseApplication;
    }

    public ExecutorService getExecutorService(){
        return initClass.getExecutorService();
    }

}
